package com.config;

import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.support.TransactionTemplate;

import javax.sql.DataSource;

public class TransactionConfig {
//将数据源放入了事务管理器 所以 增删改等操作如果 在事务中就有事务特性，操作增删改的方法添加 事务注解
    @Bean("transactionManager")
    public DataSourceTransactionManager getDataSourceTransactionManager(DataSource dataSource){
        DataSourceTransactionManager dataSourceTransactionManager =  new DataSourceTransactionManager();
        dataSourceTransactionManager.setDataSource(dataSource);
        return  dataSourceTransactionManager;
    }

    @Bean("transactionTemplate")
    public TransactionTemplate getTransactionTemplate(DataSourceTransactionManager transactionManager){
        TransactionTemplate transactionTemplate  = new TransactionTemplate();
        transactionTemplate.setTransactionManager(transactionManager);
        return transactionTemplate;
    }
}
